package com.crm.crm.controller;

import com.crm.crm.domain.vo.TreeSelectVo;
import com.crm.crm.domain.vo.business.result.Result;
import com.crm.crm.mapper.UserRoleMapper;
import com.crm.crm.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

import org.apache.commons.io.IOUtils; // 若使用IOUtils

import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
public class SystemController {
    @Autowired
    private SystemService systemService;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }

    @GetMapping("/system/menu/list")
    public String getMenuList() throws IOException {
        // 读取classpath下的JSON文件
        ClassPathResource resource = new ClassPathResource("static/data.json");
        // 将文件内容转为字符串
        return IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
    }

    //id查询所有的菜单
    @GetMapping("/system/menu/roleMenuTreeselect/{id}")
    public Result getRoleMenuTreeselect(@PathVariable("id") Long id){
        List<TreeSelectVo> list =systemService.findMenuTreeSelect();
        List<Long> list1 = userRoleMapper.selectMenuIdsByRoleId(id);
        return Result.success().put("menus",list).put("checkedKeys",list1);
    }

    @GetMapping("/system/config/configKey/sys.user.initPassword")
    public String getConfigKey() {
        return "123456";
    }
}
